package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;


public class LatoIntonacoObject extends JPanel {

	private int count = 0;
	private String[] elPiano = { "", "5°", "4°", "3°", "2°", "1°", "terra", "-1°", "-2°", "-3°", "-4°", "-5°", "altro" };
	private JComboBox piano = new JComboBox(elPiano);
	private boolean modPiano = false;
	private JTextField pianoOther = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private String[] elIntonaco = { "", "calce", "cementizio", "base calce", "base cementizio", "macro poroso", "micro poroso", "non rilevabile",
			"non presente", "tradizionale", "idrorepellente", "altro" };
	private String[] elPittura = { "", "silicati", "silossanica", "a calce", "elastomerica", "graffiato", "acrilica", "base quarzo", "non presente ",
			"non rilevabile", "cappotto termico", "idropittura", "altro" };

	private JComboBox pittura = new JComboBox(elPittura);
	private boolean modPittura = false;
	private JTextField pitturaOther = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField anno = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}

	};

	private JComboBox intonaco = new JComboBox(elIntonaco);
	private boolean modIntonaco = false;
	private JTextField intonacoOther = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private LatoIntonacoPanel lato;
	private RoundedLatoIntonaco latoRound = new RoundedLatoIntonaco("Interno", count, this);

	public LatoIntonacoObject(int i, LatoIntonacoPanel m) {
		super();
		count = i;
		lato = m;
		setLayout(new MigLayout("", "[]5[]5[]5[]", "[]1[]1[]"));
		setOpaque(false);

		latoRound.setPreferredSize(new Dimension(210, 60));
		latoRound.setOpaque(false);

		setCell();
		addAction();

		add(latoRound, "cell 0 0 ");
		add(intonaco, "cell 1 0 ");
		add(pittura, "cell 2  0");
		add(anno, "cell 3  0");
		add(piano, "cell 4 0");

	}

	public void remove() {
		lato.removeTabel(count);
	}

	public void setCount(int i) {
		count = i;
	}

	public void setCell() {
		pittura.setPreferredSize(new Dimension(210, 45));
		pitturaOther.setPreferredSize(new Dimension(210, 45));
		piano.setPreferredSize(new Dimension(100, 45));
		pianoOther.setPreferredSize(new Dimension(100, 45));
		anno.setPreferredSize(new Dimension(120, 45));
		intonaco.setPreferredSize(new Dimension(210, 45));
		intonacoOther.setPreferredSize(new Dimension(210, 45));

		pitturaOther.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		pianoOther.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		anno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		intonacoOther.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

		pitturaOther.setOpaque(false);
		pianoOther.setOpaque(false);
		anno.setOpaque(false);
		intonacoOther.setOpaque(false);

		pittura.setFont(new Font("Arial", Font.PLAIN, 14));
		pitturaOther.setFont(new Font("Arial", Font.PLAIN, 14));
		piano.setFont(new Font("Arial", Font.PLAIN, 14));
		pianoOther.setFont(new Font("Arial", Font.PLAIN, 14));
		anno.setFont(new Font("Arial", Font.PLAIN, 14));
		intonaco.setFont(new Font("Arial", Font.PLAIN, 14));
		intonacoOther.setFont(new Font("Arial", Font.PLAIN, 14));
	}

	public void addAction() {
		// action
		pittura.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				if (((String) pittura.getSelectedItem()).equalsIgnoreCase("altro")) {
					remove(pittura);
					add(pitturaOther, "cell 2 0");
					modPittura = true;
					paintAll(getGraphics());
				}
			}
		});

		piano.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((String) piano.getSelectedItem()).equalsIgnoreCase("altro")) {
					remove(piano);
					add(pianoOther, "cell 4 0");
					modPiano = true;
					paintAll(getGraphics());
				}
			}
		});
		intonaco.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((String) intonaco.getSelectedItem()).equalsIgnoreCase("altro")) {
					remove(intonaco);
					add(intonacoOther, "cell 1 0");
					modIntonaco = true;
					paintAll(getGraphics());
				}
			}
		});

		// mouse
		MouseAdapter mlA1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(pitturaOther);
					pittura.setSelectedIndex(0);
					add(pittura, "cell 2 0");
					modPittura = false;

					paintAll(getGraphics());
				}
			}
		};
		pitturaOther.addMouseListener(mlA1);

		MouseAdapter mlA2 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(pianoOther);
					piano.setSelectedIndex(0);
					add(piano, "cell 4 0");
					modPiano = false;

					paintAll(getGraphics());
				}
			}
		};
		pianoOther.addMouseListener(mlA2);

		MouseAdapter mlA3 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(intonacoOther);
					intonaco.setSelectedIndex(0);
					add(intonaco, "cell 1 0");
					modIntonaco = false;

					paintAll(getGraphics());
				}
			}
		};
		intonacoOther.addMouseListener(mlA3);

	}

	public String print() {
		String print = "#" + latoRound.getPiano();

		if (modIntonaco)
			print = print + "\n#" + intonacoOther.getText();
		else
			print = print + "\n#" + intonaco.getSelectedIndex();

		if (modPittura)
			print = print + "\n#" + pitturaOther.getText();
		else
			print = print + "\n#" + pittura.getSelectedIndex();
		print = print + "\n#" + anno.getText();

		if (modPiano)
			print = print + "\n#" + pianoOther.getText();
		else
			print = print + "\n#" + piano.getSelectedIndex();

		return print;
	}

	public void setElement(String data) {
		// System.out.println("@@ " + data);
		Scanner c = new Scanner(data);
		// c.nextLine();
		String tmp = c.nextLine();

		try {
			latoRound.setPiano(Integer.parseInt(tmp));

		} catch (NumberFormatException e) {

		}

		tmp = c.nextLine();
		try {
			intonaco.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			intonacoOther.setText(tmp);
			remove(intonaco);
			add(intonacoOther, "cell 1 0");
			modIntonaco = true;
		}

		tmp = c.nextLine();
		try {
			pittura.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			pitturaOther.setText(tmp);
			remove(pittura);
			add(pitturaOther, "cell 2 0");
			modPittura = true;
		}
		try {
			anno.setText(c.nextLine());
		} catch (Exception e) {
			anno.setText("");
		}

		tmp = c.nextLine();
		try {
			piano.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			pianoOther.setText(tmp);
			remove(piano);
			add(pianoOther, "cell 4 0");
			modPiano = true;
		}
	}

}
